package com.ubs.web.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

//TODO: add multi-language support here
public class AlertDialogBox extends DialogBox {
	
	private static final AlertDialogBox DIALOG_BOX = new AlertDialogBox();
	
	private static final String DEFAULT_ERROR_MSG = 
		"An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
	private final Label errorMessageLabel = new Label();
	
	private AlertDialogBox() {
		super(false, true);
		setTitle("Error dialog");
		final Button closeButton = new Button("Close");
		
		VerticalPanel panel = new VerticalPanel();
		panel.setSize("300", "200");
		panel.add(errorMessageLabel);
		
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setSize("300px", "300px");
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(panel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.setVerticalAlignment(VerticalPanel.ALIGN_BOTTOM);
		dialogVPanel.add(closeButton);
		setWidget(dialogVPanel);
		
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
	}
	
	public static void showError(String message) {
		DIALOG_BOX.errorMessageLabel.setText(message);
		DIALOG_BOX.center();
		DIALOG_BOX.show();
	}

	public static void showDefaultErrMessage() {
		DIALOG_BOX.errorMessageLabel.setText(DEFAULT_ERROR_MSG);
		DIALOG_BOX.center();
		DIALOG_BOX.show();
	}

	public static void showInfo(String message) {
		DIALOG_BOX.errorMessageLabel.setText(message);
		DIALOG_BOX.center();
		DIALOG_BOX.show();
	}
}
